package com.kinon.tyjaccount.fragment;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.kinon.tyjaccount.Constatus;
import com.kinon.tyjaccount.R;
import com.kinon.tyjaccount.tools.HttpTools;
import com.kinon.tyjaccount.tools.MyShowDialog;
import com.kinon.tyjaccount.tools.NetWorkUtil;
import com.kinon.tyjaccount.tools.ToastUtil;
import com.kinon.tyjaccount.view.JiesuanjineActivity;
import com.loopj.android.http.JsonHttpResponseHandler;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import java.math.BigDecimal;

/**结算*/

public class JiesuanFragment extends Fragment{
	
	private Button btn_9;
	private Button btn_8;
	private Button btn_7;
	private Button btn_6;
	private Button btn_5;
	private Button btn_4;
	private Button btn_3;
	private Button btn_2;
	private Button btn_1;
	private Button btn_0;
	private Button btn_jia;
	private Button btn_dian;
	private Button btn_qingchu;
	private Button btn_wancheng;
	private EditText et_jiesuan;
	private TextView tv_jiesuan;

	private double num1;
	private double num2;
	private double result=0.00;
	double total = 0.00;
	String str="";
	String str2="";
	HttpTools httptools=null;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.fragment_jiesuan, null);
	}
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		initRes();
	}
	private void initRes() {
		btn_9=(Button)getView().findViewById(R.id.btn_9);
		btn_8=(Button)getView().findViewById(R.id.btn_8);
		btn_7=(Button)getView().findViewById(R.id.btn_7);
		btn_6=(Button)getView().findViewById(R.id.btn_6);
		btn_5=(Button)getView().findViewById(R.id.btn_5);
		btn_4=(Button)getView().findViewById(R.id.btn_4);
		btn_3=(Button)getView().findViewById(R.id.btn_3);
		btn_2=(Button)getView().findViewById(R.id.btn_2);
		btn_1=(Button)getView().findViewById(R.id.btn_1);
		btn_0=(Button)getView().findViewById(R.id.btn_0);
		btn_jia=(Button)getView().findViewById(R.id.btn_jia);
		btn_dian=(Button)getView().findViewById(R.id.btn_dian);
		btn_qingchu=(Button)getView().findViewById(R.id.btn_qingchu);
		btn_wancheng=(Button)getView().findViewById(R.id.btn_wancheng);
		et_jiesuan=(EditText)getView().findViewById(R.id.et_jiesuan);
		tv_jiesuan=(TextView)getView().findViewById(R.id.tv_jiesuan);
		btn_9.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				str = str +"9";
				str2 = str2 +"9";
				et_jiesuan.setText(str);
				tv_jiesuan.setText(str2);
			}
		});
		btn_8.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				str = str +"8";
				str2 = str2 +"8";
				et_jiesuan.setText(str);
				tv_jiesuan.setText(str2);
			}
		});
		btn_7.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				str = str +"7";
				str2 = str2 +"7";
				et_jiesuan.setText(str);
				tv_jiesuan.setText(str2);
			}
		});
		btn_6.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				str = str +"6";
				str2 = str2 +"6";
				et_jiesuan.setText(str);
				tv_jiesuan.setText(str2);
			}
		});
		btn_5.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				str = str +"5";
				str2 = str2 +"5";
				et_jiesuan.setText(str);
				tv_jiesuan.setText(str2);
			}
		});
		btn_4.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				str = str +"4";
				str2 = str2 +"4";
				et_jiesuan.setText(str);
				tv_jiesuan.setText(str2);
			}
		});
		btn_3.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				str = str +"3";
				str2 = str2 +"3";
				et_jiesuan.setText(str);
				tv_jiesuan.setText(str2);
			}
		});
		btn_2.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				str = str +"2";
				str2 = str2 +"2";
				et_jiesuan.setText(str);
				tv_jiesuan.setText(str2);
			}
		});
		btn_1.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				str = str +"1";
				str2 = str2 +"1";
				et_jiesuan.setText(str);
				tv_jiesuan.setText(str2);
			}
		});
		btn_0.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				str = str +"0";
				str2 = str2 +"0";
				et_jiesuan.setText(str);
				tv_jiesuan.setText(str2);
			}
		});
		btn_jia.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
//				str = str +"00";
//				et_jiesuan.setText(str);
				if (str.equals("")){
					Toast.makeText(getActivity(),"请输入金额!",Toast.LENGTH_SHORT).show();
					return;
				}
				if (str.equals(".")) {
					Toast.makeText(getActivity(), "请输入正确的金额!", Toast.LENGTH_SHORT).show();
					return;
				}
				str2=str2+"+";
				num1=Double.parseDouble(et_jiesuan.getText().toString());
//				result=num1;
				total+=num1;
				result=total;
				str="";
				et_jiesuan.setText(""+total);
				tv_jiesuan.setText(str2);
				Log.e("zq","num1--"+num1);
				Log.e("zq","result--"+result);
			}
		});
		btn_dian.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				str = str +".";
				str2 = str2 +".";
				et_jiesuan.setText(str);
				tv_jiesuan.setText(str2);
			}
		});
		btn_wancheng.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
//				//判断是否是紧急状态
				if(!Constatus.URGENT_TYPE){
					Saveorder();
				}else{
					urgentJiesuan();
				}
			}
		});
		btn_qingchu.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				str = "";
				total=0.0;
				result=0.0;
				num1=0.0;
				num2=0.0;
				str2 = "";
				et_jiesuan.setText(str);
				tv_jiesuan.setText(str2);
			}
		});
	}

	/**紧急结算*/
	private void urgentJiesuan (){
		try{
			num2=Double.parseDouble(et_jiesuan.getText().toString());
			result+=num2;
			Intent intent = new Intent(getActivity(), JiesuanjineActivity.class);
			intent.putExtra("jine",result);
			startActivity(intent);
			reset();
		}catch (Exception e){
			Toast.makeText(getActivity(),"请输入正确的金额",Toast.LENGTH_SHORT).show();
			e.printStackTrace();
		}

	}



	/**提交数据*/
	private void Saveorder() {
		if(NetWorkUtil.isNetwork(getActivity())==false)
		{
            ToastUtil toastUtil=new ToastUtil();
            toastUtil.Short(getActivity(),"  "+"当前网络不可用")
                    .setToastBackground(Color.WHITE,0).show();
//			Toast.makeText(getActivity(), "当前网络不可用", Toast.LENGTH_SHORT).show();
			return;
		}
		btn_wancheng.setEnabled(false);
//		infolist=new ArrayList<CaidanEntity>();
		SharedPreferences sp = getActivity().getSharedPreferences("spReg",
				Context.MODE_PRIVATE);
		String token = sp.getString("data", "");
		if (str.equals("")) {
			Toast.makeText(getActivity(), "请输入金额!", Toast.LENGTH_SHORT).show();
			btn_wancheng.setEnabled(true);
			return;
		}
		if (str.equals(".")) {
			Toast.makeText(getActivity(), "请输入正确的金额!", Toast.LENGTH_SHORT).show();
			btn_wancheng.setEnabled(true);
			return;
		}

//		tv_jiesuan.setText(str2);
		num2=Double.parseDouble(et_jiesuan.getText().toString());
//				result = num1+num2;
		result+=num2;
		et_jiesuan.setText(""+result);
		str="";
		if (result==0.0) {
			Toast.makeText(getActivity(), "请输入大于0的金额!", Toast.LENGTH_SHORT).show();
			btn_wancheng.setEnabled(true);
			return;
		}
		if (httptools == null) {
			httptools = new HttpTools(getActivity());
		}
		httptools.saveNewOrder(token,""+result,new JsonSaveorder());

	}

	public class JsonSaveorder extends JsonHttpResponseHandler {
		@Override
		public void onStart() {
			MyShowDialog.showLoadingDialog(getActivity(), "订单生成中....");
			super.onStart();
		}

		@Override
		public void onSuccess(int statusCode, Header[] headers,
				JSONObject response) {
			super.onSuccess(statusCode, headers, response);
			MyShowDialog.closeLoadingDialog();
			try {
				boolean code = response.getBoolean("code");
				String msg = response.getString("msg");
				if (code == true) {
					String orderid = response.getString("data");
					Intent intent = new Intent();
					intent.setClass(getActivity(), JiesuanjineActivity.class);
					intent.putExtra("orderid", orderid);
					BigDecimal bg = new BigDecimal(result);
					double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
					intent.putExtra("jine",f1);
					startActivity(intent);
					Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
					reset();
				}else {
					btn_wancheng.setEnabled(true);
					Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
				}

			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}



		@Override
		public void onFailure(Throwable e, JSONObject errorResponse) {
			super.onFailure(e, errorResponse);
			MyShowDialog.closeLoadingDialog();
			Toast.makeText(getActivity(), "网络请求失败", Toast.LENGTH_SHORT).show();
			reset();
		}
	}

	private void reset(){
		total=0.0;
		result=0.0;
		num1=0.0;
		num2=0.0;
		str="";
		str2="";
		et_jiesuan.setText(str);
		tv_jiesuan.setText(str2);
		btn_wancheng.setEnabled(true);
	}

}
